package and.calc;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SimpleViewClass extends Activity 
{
	EditText calcText;
	Button num0;
	Button num1;
	Button num2;
	Button num3;
	Button num4;
	Button num5;
	Button num6;
	Button num7;
	Button num8;
	Button num9;
	Button dotButt;
	Button addButt;
	Button minButt;
	Button mulButt;
	Button divButt;
	Button equButt;
	String enteredText;
	boolean operationSet;
	
	Calculator calc;
	
	int MAX_TEXT_LENGTH = 13;		// Maximum Number of Characters 
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simpleview);
        

        
        
        calc = new Calculator();
        operationSet = false;
        
        calcText = (EditText) findViewById(R.id.simpCalcText);
        
        // Implement Click Functions For Buttons
        // 1- Implement Functions For Digits 
        num0 = (Button) findViewById(R.id.num0);
        num0.setOnClickListener(new OnClickListener() {
			
        	
        	//// The Same Comments applies to All Digits Functions 
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				/// If the Text in Text Box Reached the MAX then Do Nothing
				if(enteredText.length() == MAX_TEXT_LENGTH)
				{
					return;
				}
				
				
				/// If the Text Length is One then 
				/// if the text is not Zero Then Add Zero to Text Box if the operation wasn't set
				/// else replace the Text With Zero
				if(enteredText.length() == 1)
				{
					if(!enteredText.contains("0"))
					{
						if(operationSet)
							calcText.setText("0");
						else
							calcText.setText(calcText.getText() + "0");						
					}
				}
				else
				{
					if(operationSet)
						calcText.setText("0");
					else
						calcText.setText(calcText.getText() + "0");
				}												
			}
		});
        
        num1 = (Button) findViewById(R.id.num1);
        num1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(enteredText.length() == MAX_TEXT_LENGTH)
				{
					return;
				}
								
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("1");			
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "1");
				}			
			}
		});
        
        
        num2 = (Button) findViewById(R.id.num2);
        num2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(enteredText.length() == MAX_TEXT_LENGTH)
				{
					return;
				}
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("2");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "2");
				}					
			}
		});
        
        num3 = (Button) findViewById(R.id.num3);
        num3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(enteredText.length() == MAX_TEXT_LENGTH)
				{
					return;
				}
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("3");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "3");
				}
							
			}
		});
        
        num4 = (Button) findViewById(R.id.num4);
        num4.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(enteredText.length() == MAX_TEXT_LENGTH)
				{
					return;
				}
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("4");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "4");
				}			
			}
		});
        
        num5 = (Button) findViewById(R.id.num5);
        num5.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(enteredText.length() == MAX_TEXT_LENGTH)
				{
					return;
				}
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("5");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "5");
				}			
			}
		});
        
        num6 = (Button) findViewById(R.id.num6);
        num6.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(enteredText.length() == MAX_TEXT_LENGTH)
				{
					return;
				}
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("6");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "6");
				}			
			}
		});
        
        num7 = (Button) findViewById(R.id.num7);
        num7.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(enteredText.length() == MAX_TEXT_LENGTH)
				{
					return;
				}
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("7");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "7");
				}		
			}
		});
        
        num8 = (Button) findViewById(R.id.num8);
        num8.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(enteredText.length() == MAX_TEXT_LENGTH)
				{
					return;
				}
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("8");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "8");
				}			
			}
		});
        
        num9 = (Button) findViewById(R.id.num9);
        num9.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(enteredText.length() == MAX_TEXT_LENGTH)
				{
					return;
				}
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("9");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "9");
				}			
			}
		});
        
        dotButt = (Button) findViewById(R.id.dotSign);
        dotButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(enteredText.length() == MAX_TEXT_LENGTH)
				{
					return;
				}
				
				if(!(calcText.getText().toString().contains(".")))
					calcText.setText(calcText.getText() + ".");				
			}
		});
        
        
        
        /// Functions Buttons
        
        /// Add Button
        addButt = (Button) findViewById(R.id.addSign);
        addButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString();
				
				calc.setM_number1(Float.parseFloat(enteredText));
				calc.setOperation(Operations.add);
				
				operationSet = true;
				
			}
		});
        
        /// Subtraction Button
        minButt = (Button) findViewById(R.id.minusSign);
        minButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString();
				
				calc.setM_number1(Float.parseFloat(enteredText));
				calc.setOperation(Operations.sub);
				
				operationSet = true;
				
			}
		});
        
        /// Multiplication Button	
        mulButt = (Button) findViewById(R.id.multSign);
        mulButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				enteredText = calcText.getText().toString();
				
				calc.setM_number1(Float.parseFloat(enteredText));
				calc.setOperation(Operations.mul);
				
				operationSet = true;
				
			}
		});
        
        /// Division Button
        divButt = (Button) findViewById(R.id.divSign);
        divButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				enteredText = calcText.getText().toString();
				
				calc.setM_number1(Float.parseFloat(enteredText));
				calc.setOperation(Operations.div);
				
				operationSet = true;
			}
		});
        
        
        /// Equal Button
        equButt = (Button) findViewById(R.id.equalSign);
        equButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString();
				calc.setM_number2(Float.parseFloat(enteredText));
				calcText.setText(String.valueOf(calc.getM_result()));
			}
		});
        
        
    }

	
    
    
    ///// Menu Handling 
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.simple_menu, menu);
    	
		return true;
	}




	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
	
		if(item.getItemId() == R.id.clearItem)
		{
			calcText.setText("0");
			
			calc.setM_number1(0);
			calc.setM_number2(0);
			calc.setOperation(Operations.none);
			
		}
		else if(item.getItemId() == R.id.removeItem)
		{
			int length = calcText.getText().toString().length();
			
			
			if(length == 0)
			{
				// Do Nothing
			}
			else if(length == 1)
			{		
				calcText.setText("0");				
			}
			else
			{
				calcText.setText(calcText.getText().toString().substring(0, calcText.getText().toString().length()-1));
			}
			
				
		}
		
		
		return true;
	}
        
    
    
    
    
}
